//
//  ViewController.m
//  保存数据
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *NameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=NSHomeDirectory();
    NSLog(@"%@",path);
    //得到沙盒目录下的文件夹数组
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath1=paths[0];
    NSLog(@"%@",docPath1);
    
    //沙盒目录NSHomeDirectory()
    NSString *docPath2=[NSHomeDirectory() stringByAppendingString:@"/Documents"];
    NSLog(@"%@",docPath2);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - NSUserDefault方式
#define kStuId @"StuId"
#define kStuName @"StuName"
#define kStuAge @"StuAge"
- (IBAction)save1BtnPressed:(id)sender {
    //得到NSUserDefault单例对象
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    [d setObject:self.idTextField.text forKey:kStuId];
    [d setObject:self.NameTextField.text forKey:kStuName];
    [d setObject:self.ageTextField.text forKey:kStuAge];
    
    [d synchronize];
    
}
- (IBAction)read1BtnPressed:(id)sender {
    
    NSUserDefaults *d=[NSUserDefaults standardUserDefaults];
    self.idTextField.text=[d objectForKey:kStuId];
    self.NameTextField.text=[d objectForKey:kStuName];
    self.ageTextField.text=[d objectForKey:kStuAge];
    
}


#pragma mark - plist方式 (信息存入数组或者字典)
- (IBAction)save2BtnPressed:(id)sender {
    NSDictionary *dict=@{@"stuId":self.idTextField.text,@"stuName":self.NameTextField.text,
                         @"stuAge":self.ageTextField.text};
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    NSLog(@"存到：%@",filePath);
    
    [dict writeToFile:filePath atomically:YES];
}
- (IBAction)read2BtnPressed:(id)sender {
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.plist"];
    NSLog(@"读取:%@",filePath);
    NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.idTextField.text=dict[@"stuId"];
    self.NameTextField.text=dict[@"stuName"];
    self.ageTextField.text=dict[@"stuAge"];
}

#pragma mark - 归档方式 (数据放入支持归档的对象)
- (IBAction)save3BtnPressed:(id)sender {
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.arch"];
    StudentModel *m=[[StudentModel alloc]initWithId:self.idTextField.text andName:self.NameTextField.text andAge:self.ageTextField.text];
    NSMutableData *mData=[NSMutableData data];
    NSKeyedArchiver *arch=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    
    [arch encodeObject:m forKey:@"Student"];
    [arch finishEncoding];
    
    [mData writeToFile:filePath atomically:YES];
    
    
}

- (IBAction)read3Pressed:(id)sender {
    NSString *docPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath=[docPath stringByAppendingPathComponent:@"student.arch"];
    
    NSData *data=[NSData dataWithContentsOfFile:filePath];
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    StudentModel *stu = [unArch decodeObjectForKey:@"Student"];
    [unArch finishDecoding];
    
    self.idTextField.text=stu.stuId;
    self.NameTextField.text=stu.stuName;
    self.ageTextField.text=stu.stuAge;
}
- (IBAction)backTap:(id)sender {
    
    [self.view endEditing:YES];
}

@end
